package view;

import javax.swing.JPanel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import model.kwalificatiebeheer.Kerntaak;
import model.kwalificatiebeheer.KwalificatieDossier;

import java.sql.Date;
import java.util.Observable;
import java.util.Observer;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import controller.KerntaakTableActions;
import controller.KwalificatieDossierPanelActions;

import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.border.EtchedBorder;

public class KwalificatieDossierPanel extends JPanel implements Observer{
	private JTable table;
	private JPanel mainPanel;
	private KwalificatieDossier active;
	private Kerntaak activeKt;
	private JTextField textStartDateField;
	private JTextField textSectorField;
	private JTextField textBranchField;
	private JTextField textDomainField;
	private JTextField textCreboField;
	private JTable tableKerntaken;
	private GridBagConstraints gbc_mainPanel;
	
	public KwalificatieDossierPanel() {
		setVisible(true);
	
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{100, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JPanel leftSidePanel = new JPanel();
		leftSidePanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		GridBagConstraints gbc_leftSidePanel = new GridBagConstraints();
		gbc_leftSidePanel.insets = new Insets(0, 0, 0, 5);
		gbc_leftSidePanel.fill = GridBagConstraints.BOTH;
		gbc_leftSidePanel.gridx = 0;
		gbc_leftSidePanel.gridy = 0;
		add(leftSidePanel, gbc_leftSidePanel);
		GridBagLayout gbl_leftSidePanel = new GridBagLayout();
		gbl_leftSidePanel.columnWidths = new int[]{0, 100, 0, 0};
		gbl_leftSidePanel.rowHeights = new int[]{0, 30, 30, 0};
		gbl_leftSidePanel.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_leftSidePanel.rowWeights = new double[]{1.0, 0.0, 0.0, 1.0};
		leftSidePanel.setLayout(gbl_leftSidePanel);
		
		JButton btnLijst = new JButton("Lijst");
		btnLijst.addActionListener(new KwalificatieDossierPanelActions(this));
		
		JLabel lblKwalificatieDossier = new JLabel("Kwalificatie Dossier");
		GridBagConstraints gbc_lblKwalificatieDossier = new GridBagConstraints();
		gbc_lblKwalificatieDossier.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblKwalificatieDossier.anchor = GridBagConstraints.NORTH;
		gbc_lblKwalificatieDossier.insets = new Insets(0, 0, 5, 5);
		gbc_lblKwalificatieDossier.gridx = 1;
		gbc_lblKwalificatieDossier.gridy = 0;
		leftSidePanel.add(lblKwalificatieDossier, gbc_lblKwalificatieDossier);
		
		btnLijst.setActionCommand("btnLijst");
		GridBagConstraints gbc_btnLijst = new GridBagConstraints();
		gbc_btnLijst.insets = new Insets(0, 0, 5, 5);
		gbc_btnLijst.fill = GridBagConstraints.BOTH;
		gbc_btnLijst.gridx = 1;
		gbc_btnLijst.gridy = 1;
		leftSidePanel.add(btnLijst, gbc_btnLijst);
		
		JButton btnNieuw = new JButton("Nieuw");
		btnNieuw.addActionListener(new KwalificatieDossierPanelActions(this));
		btnNieuw.setActionCommand("btnNieuw");
		GridBagConstraints gbc_btnNieuw = new GridBagConstraints();
		gbc_btnNieuw.insets = new Insets(0, 0, 5, 5);
		gbc_btnNieuw.fill = GridBagConstraints.BOTH;
		gbc_btnNieuw.gridx = 1;
		gbc_btnNieuw.gridy = 2;
		leftSidePanel.add(btnNieuw, gbc_btnNieuw);
		
		Component verticalStrut = Box.createVerticalStrut(20);
		GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
		gbc_verticalStrut.insets = new Insets(0, 0, 0, 5);
		gbc_verticalStrut.gridx = 0;
		gbc_verticalStrut.gridy = 3;
		leftSidePanel.add(verticalStrut, gbc_verticalStrut);
		
		Component verticalStrut_1 = Box.createVerticalStrut(20);
		GridBagConstraints gbc_verticalStrut_1 = new GridBagConstraints();
		gbc_verticalStrut_1.gridx = 2;
		gbc_verticalStrut_1.gridy = 3;
		leftSidePanel.add(verticalStrut_1, gbc_verticalStrut_1);
		
		mainPanel = new JPanel();
		mainPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		gbc_mainPanel = new GridBagConstraints();
		gbc_mainPanel.fill = GridBagConstraints.BOTH;
		gbc_mainPanel.gridx = 1;
		gbc_mainPanel.gridy = 0;
		add(mainPanel, gbc_mainPanel);
		GridBagLayout gbl_mainPanel = new GridBagLayout();
		gbl_mainPanel.columnWidths = new int[]{61, 0};
		gbl_mainPanel.rowHeights = new int[]{15, 0, 0};
		gbl_mainPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_mainPanel.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		mainPanel.setLayout(gbl_mainPanel);
		
		initTable();
		//initForm("display");
	}
	
	public void initTable(){
		KwalificatieDossierTableModel kdtm = new KwalificatieDossierTableModel();
		table = new JTable(kdtm);
		table.setShowVerticalLines(false);
		table.addMouseListener(new KwalificatieDossierPanelActions(this));
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridx = 0;
		gbc_table.gridy = 0;
		mainPanel.removeAll();
		mainPanel.add(new JScrollPane(table), gbc_table);
		
		JButton btnRemoveKd = new JButton("Verwijderen");
		btnRemoveKd.setActionCommand("removeKd");
		GridBagConstraints gbc_btnRemoveKd = new GridBagConstraints();
		gbc_btnRemoveKd.anchor = GridBagConstraints.WEST;
		gbc_btnRemoveKd.gridx = 0;
		gbc_btnRemoveKd.gridy = 1;
		mainPanel.add(btnRemoveKd, gbc_btnRemoveKd);
		mainPanel.revalidate();
	}
	
	public void initForm(String mode){
		KwalificatieDossier kd = new KwalificatieDossier();
		if(mode == "edit" || mode == "display"){
			if(active != null){
				kd = active;
			}else{
				initTable();
			}
		}
		
		JPanel formPanel = new JPanel();
		formPanel.setVisible(true);
		GridBagConstraints gbc_formPanel = new GridBagConstraints();
		gbc_formPanel.fill = GridBagConstraints.BOTH;

		mainPanel.removeAll();
		mainPanel.add(formPanel, gbc_formPanel);
		mainPanel.revalidate();
		
		formPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("fill:min"),
				RowSpec.decode("4dlu:grow"),
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblIngangsDatum = new JLabel("Ingangs datum:");
		formPanel.add(lblIngangsDatum, "4, 4");
		if(mode == "display"){
			JLabel lblIngangsDatumValue = new JLabel(String.valueOf(new Date(kd.getCohort()*1000L)));
			formPanel.add(lblIngangsDatumValue, "8, 4, fill, default");
		}else{
			textStartDateField = new JTextField();
			if(mode ==  "edit"){
				textStartDateField.setText(String.valueOf(kd.getCohort()));
			}
			formPanel.add(textStartDateField, "8, 4, fill, default");
			textStartDateField.setColumns(10);
		}
		
		JLabel lblSector = new JLabel("Sector:");
		formPanel.add(lblSector, "4, 6");
		if(mode == "display"){
			JLabel lblSectorValue = new JLabel(kd.getSector());
			formPanel.add(lblSectorValue, "8, 6, fill, default");
		}else{
			textSectorField = new JTextField();
			if(mode ==  "edit"){
				textSectorField.setText(kd.getSector());
			}
			formPanel.add(textSectorField, "8, 6, fill, default");
			textSectorField.setColumns(10);
		}
		
		JLabel lblBranch = new JLabel("Branch:");
		formPanel.add(lblBranch, "4, 8");
		if(mode == "display"){
			JLabel lblBranchValue = new JLabel(kd.getBranch());
			formPanel.add(lblBranchValue, "8, 8, fill, default");
		}else{
			textBranchField = new JTextField();
			if(mode ==  "edit"){
				textBranchField.setText(kd.getBranch());
			}
			formPanel.add(textBranchField, "8, 8, fill, default");
			textBranchField.setColumns(10);
		}
		
		JLabel lblDomein = new JLabel("Opleidings Domein:");
		formPanel.add(lblDomein, "4, 10");
		if(mode == "display"){
			JLabel lblDomeinValue = new JLabel(kd.getDomain());
			formPanel.add(lblDomeinValue, "8, 10, fill, default");
		}else{
			textDomainField = new JTextField();
			if(mode ==  "edit"){
				textDomainField.setText(kd.getDomain());
			}
			formPanel.add(textDomainField, "8, 10, fill, default");
			textDomainField.setColumns(10);
		}
		
		JLabel lblCrebos;
		if(mode == "display"){
			lblCrebos = new JLabel("Crebonummer:");
		}else{
			lblCrebos = new JLabel("Crebonummer (scheiden met \",\"):");
		}
		formPanel.add(lblCrebos, "4, 12");
		
		String crebos = "";
		if(kd.getCrebos() != null){
			Long[] creboList = kd.getCrebos();
	    	for(int i =0; i < creboList.length; i++){
	    		if(i > 0){
	        		crebos = crebos + ", " + creboList[i];
	    		}else{
	    			crebos = ""+creboList[i];
	    		}
	    	}
		}
			
		if(mode == "display"){
			JButton btnEdit = new JButton("Bewerken");
			btnEdit.addActionListener(new KwalificatieDossierPanelActions(this));
			btnEdit.setActionCommand("btnEdit");
			formPanel.add(btnEdit, "8, 12, right, center");	
		}else{
			textCreboField = new JTextField();
			if(mode ==  "edit"){
			textCreboField.setText(crebos);
			}
			formPanel.add(textCreboField, "8, 12, fill, default");
			textCreboField.setColumns(10);
		}
		
		if(mode == "display"){
			JLabel lblCreboValue = new JLabel(crebos);
			formPanel.add(lblCreboValue, "8, 12, fill, default");
		}else{
			JButton btnSaveNew = new JButton("Opslaan");
			btnSaveNew.addActionListener(new KwalificatieDossierPanelActions(this));
			if(mode == "new"){
				btnSaveNew.setActionCommand("btnSaveNew");
			}else if(mode == "edit"){
				btnSaveNew.setActionCommand("btnSaveEdit");
				
				JButton btnCancel = new JButton("Annuleren");
				btnCancel.addActionListener(new KwalificatieDossierPanelActions(this));
				btnCancel.setActionCommand("btnCancel");
				formPanel.add(btnCancel, "8, 14, center, center");	
			}
			formPanel.add(btnSaveNew, "8, 14, right, center");			
		}
		
		if(mode == "display"){	
			JLabel lblKerntaken = new JLabel("Kerntaken:");
			formPanel.add(lblKerntaken, "4, 14");
			tableKerntaken = new JTable(new KerntaakTableModel(active));
			tableKerntaken.addMouseListener(new KerntaakTableActions(this, tableKerntaken));
			tableKerntaken.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			formPanel.add(new JScrollPane(tableKerntaken), "4, 15, 5, 1, fill, fill");
			JButton btnToevoegen = new JButton("Toevoegen");
			btnToevoegen.addActionListener(new KwalificatieDossierPanelActions(this));
			btnToevoegen.setActionCommand("addKerntaak");
			formPanel.add(btnToevoegen, "8, 16, right, default");
			
			JButton btnVerwijderen = new JButton("Verwijderen");
			btnVerwijderen .addActionListener(new KwalificatieDossierPanelActions(this));
			btnVerwijderen .setActionCommand("removeKerntaak");
			formPanel.add(btnVerwijderen , "4, 16, left, default");
		}
	}
	
	public void deleteActive(){
		
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {
		if(active != null){
			for(Kerntaak kt : active.getKerntaken()){
				System.out.println(kt);
			}
			tableKerntaken.setModel(new KerntaakTableModel(active));
		}
		
	}

	/**
	 * Getters And Setters
	 */
	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

	public JPanel getMainPanel() {
		return mainPanel;
	}

	public void setMainPanel(JPanel mainPanel) {
		if (this.mainPanel != null) {
			remove(this.mainPanel);
		}
		this.mainPanel = mainPanel;
		add(this.mainPanel, gbc_mainPanel);
		revalidate();
	}

	public Kerntaak getActiveKt() {
		return activeKt;
	}

	public void setActiveKt(Kerntaak activeKt) {
		this.activeKt = activeKt;
	}
	
	public KwalificatieDossier getActive() {
		return active;
	}

	public void setActive(
			KwalificatieDossier active) {
		if(this.active != null){
			this.active.deleteObserver(this);
		}
		
		active.addObserver(this);
		this.active = active;
	}

	public JTextField getTextStartDateField() {
		return textStartDateField;
	}

	public JTextField getTextSectorField() {
		return textSectorField;
	}

	public JTextField getTextBranchField() {
		return textBranchField;
	}

	public JTextField getTextDomainField() {
		return textDomainField;
	}

	public JTextField getTextCreboField() {
		return textCreboField;
	}

	public JTable getTableKerntaken() {
		return tableKerntaken;
	}
}
